package com.example.controller.institutioncare.jingfeibutie;

import com.example.bean.institutioncare.institutionadmin.Basicdata;
import com.example.bean.institutioncare.jingfeibutie.JingFeiBuTie;
import com.example.bean.institutioncare.jingfeibutie.JingFeiBuTieVo;
import com.example.service.institutioncare.jingfeibutie.JingFeiBuTieService;
import com.example.utils.Result;
import com.github.pagehelper.PageInfo;
import lombok.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/jingfeibutie")
public class JingFeiBuTieController {

    @Autowired
    JingFeiBuTieService jingFeiBuTieService;

    @PostMapping("/page")
    public PageInfo<JingFeiBuTie> jingfeipage(@RequestBody JingFeiBuTieVo jingFeiBuTieVo){
        System.out.println(jingFeiBuTieVo);
        return jingFeiBuTieService.jingfeifenye(jingFeiBuTieVo);
    }

    @PostMapping("/jibenbiao")
    public List<Basicdata> jibenbiao(){
        List<Basicdata> list = jingFeiBuTieService.jibenbiao();
        return list;
    }

    @PostMapping("/addjingfei")
    public Result addJingFei(@RequestBody JingFeiBuTie jingFeiBuTie){
        Result result = new Result();
        result.setCode(jingFeiBuTieService.tianjiajingfei(jingFeiBuTie));
        return result;
    }

    //文件上传，将传来的图片保存到本地，将文件名称存入数据库中
    @PostMapping("/upload")
    public Map<String,Object> fileupload(MultipartFile file, HttpServletRequest req) {
        Map<String, Object> result = new HashMap<>();

        // 放在本地项目 目录
        //D:\ProjectTraining\健康证管理系统\images
        String filePath="C:\\images";
        File folder = new File(filePath);

        if (!folder.exists()) {
            folder.mkdirs();
        }
        // 生成新的文件名
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        System.out.println("新生成的文件名："+newName);

        try {
            // 保存文件到指定路径
            file.transferTo(new File(folder, newName));
            //String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort()+"/img/"  + newName;

            //System.out.println("路径地址："+url);
            result.put("status", "OK");
            result.put("name", newName);
            //result.put("url", url);

        } catch (IOException e) {
            result.put("status", "ERROR");
            result.put("msg", e.getMessage());
        }
        System.out.println("图片上传："+result);
        return result;
    }

    @GetMapping("/{noid}")
    public Result huixian(@PathVariable String noid){
        JingFeiBuTie jingFeiBuTie = jingFeiBuTieService.jingfeihuixian(noid);
        Result r = new Result();
        if (jingFeiBuTie != null){
            r.ok();
            r.setData(jingFeiBuTie);
        }else {
            r.fail("失败");
        }
        return r;
    }

    @PostMapping("/xiugaijingfei")
    public Result xiuJingFei(@RequestBody JingFeiBuTie jingFeiBuTie){
        Integer jingfei = jingFeiBuTieService.xiugaijingfei(jingFeiBuTie);
        Result r = new Result();
        if (jingfei > 0){
            r.ok();
        }else {
            r.fail("修改失败");
        }
        return r;
    }


    @PostMapping("/dele")
    public Result dels(@RequestBody List<String> ids){
        Integer flag = jingFeiBuTieService.jingfeishanchu(ids);
        Result re = new Result();
        if (flag > 0){
            re.ok();
        }else {
            re.fail("删除失败");
        }
        return re;
    }


}
